//
//  SDMineViewController.m
//  skinDetection
//
//  Created by  liangzhun on 2018/10/29.
//  Copyright © 2018年  liangzhun. All rights reserved.
//

#import "SDMineViewController.h"

#import "SDAlertController.h"
#import "SDQuestionnaireViewController.h"

#import "SDMineTableViewCell.h"
#import "SDMineCellModel.h"
#import "SDMineHeaderView.h"
static NSString *const cellIden = @"SDMineTableViewCell";
static NSString *const HeaderName = @"SDMineHeaderView";
@interface SDMineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong)NSMutableArray *cellArray;/*cell显示的数据模型s的数组*/
@end

@implementation SDMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _navHeight.constant = SDNavBar_Height;
    
    [self.tableView registerNib:[UINib nibWithNibName:cellIden bundle:nil] forCellReuseIdentifier:cellIden];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    SDMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden];
    [cell setModel:self.cellArray[indexPath.row]];
    cell.skinLab.hidden = indexPath.row != 2;
    if (indexPath.row == 2) {
        NSString *skin = SDAppDelegate.userModel.skinType == 1?Little_D:SDAppDelegate.userModel.skinType == 2? Much_D:SDAppDelegate.userModel.skinType == 3? Little_O:SDAppDelegate.userModel.skinType == 4? Much_O:@"";
        [cell.skinLab setText:skin];
        cell.skinLab.hidden = skin.length == 0;
    }
    return cell;
    
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cellArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 155*SDWidth375Scale + 59 + 32;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SDMineHeaderView *headerView = [SDMethodTool getViewWithViewName:HeaderName];
    headerView.headerPhoto.layer.cornerRadius = 40.0;
    headerView.headerPhoto.layer.masksToBounds = YES;
    return headerView;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    indexPath.row == 1? [self outLogin]:[self goReportVCAndTestSkinVCWithIndexRow:indexPath];
    
}
//进入检测报告页面
- (void)goReportVCAndTestSkinVCWithIndexRow:(NSIndexPath *)indexPath
{
    NSArray *vcArray = @[@"SDMyReporViewController",@"SDUserReportViewController"];
    NSString *vc = vcArray[indexPath.row];
    [self.tabBarController.navigationController pushViewController:[SDMethodTool getViewControlWithIdentifier:vc] animated:YES];
}

/**
 进入问卷调查页面
 */
- (void)goQuestionnaireVC
{
    SDQuestionnaireViewController *vc = [SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDQuestionnaireViewController class])];
    //__weak typeof(self) weakself = self;
//    vc.block = ^{
//        
//        [self.tableView reloadData];
//        [self.tableView layoutIfNeeded];
//       
//    };
    [self.tabBarController.navigationController pushViewController:vc animated:YES];
    
}

/**
 退出登录
 */
- (void)outLogin
{
    SDAlertController *alert = [SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDAlertController class])];
    
    [alert.view setFrame:self.view.bounds];
    [self.tabBarController.view addSubview:alert.view];
    [self.tabBarController addChildViewController:alert];
    [alert setAlertContent:outLoginAlertContent Title:outLoginAlertTitle CancelTitle:outLoginAlertCancel ConfirmTitle:outLoginAlertConfirm];
    [alert.confirmBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
       
        [SDAppDelegate goLogin];
        
    }];
    
}
#pragma mark - 懒加载

- (NSMutableArray *)cellArray
{
    if (!_cellArray) {
        
        _cellArray = [[NSMutableArray alloc] initWithCapacity:0];
        NSArray *imageArray = @[@"我的报告icon",@"退出登录"];
        NSArray *titleArray = @[@"我的报告",@"退出登录"];

        for (int i = 0; i < imageArray.count; i++) {
            
            SDMineCellModel *model = [[SDMineCellModel alloc] init];
            model.icon = imageArray[i];
            model.title = titleArray[i];
            [_cellArray addObject:model];
            
        }
    }
    
    return _cellArray;
    
}
@end
